import datetime

from django.db import models

from apps.member.activity.models import Activity, MemberActivity
from apps.member.models import Member
from apps.member.stores.models import Coupon
from apps.system.models import Upload
from utils.exceptions import CustomError


class VoteActivity(models.Model):
    EVERYDAY = 1
    TOTAL = 2
    VOTE_WAY = (
        (EVERYDAY, '每天'),
        (TOTAL, '总共')
    )
    activity = models.ForeignKey(Activity, verbose_name='活动信息', on_delete=models.PROTECT, related_name='vote')
    vote_way = models.PositiveSmallIntegerField(choices=VOTE_WAY, verbose_name='投票方式', default=TOTAL)
    vote_time = models.IntegerField(verbose_name='投票次数')
    total_vote_count = models.IntegerField(verbose_name='总投票数', default=0)

    class Meta:
        db_table = 'activity_vote'
        verbose_name = '投票活动'

    def has_vote_count(self, member):
        # 已经投票次数
        if self.vote_way == VoteActivity.EVERYDAY:
            # 每天几次
            today = datetime.date.today()  # 2020-10-24
            # 查询用户当天投票次数
            vote_count = MemberVoteRecord.objects.filter(create_time__gte=today, member=member,
                                                         vote_activity=self).count()
        else:
            # 一共几次
            vote_count = MemberVoteRecord.objects.filter(member=member, vote_activity=self).count()

        return vote_count


# class VoteActivityAward(models.Model):
#     vote_activity = models.ForeignKey(VoteActivity, verbose_name='当前活动', on_delete=models.PROTECT)
#     range = models.CharField(max_length=50, verbose_name='获奖范围')
#     coupon = models.ForeignKey(Coupon, verbose_name='奖券', on_delete=models.PROTECT)
#
#     class Meta:
#         db_table = 'activity_vote_award'
#         verbose_name = '投票活动奖项'


class VoteActivityApply(models.Model):
    member_activity = models.ForeignKey(MemberActivity, verbose_name='用户活动', on_delete=models.PROTECT,
                                        related_name='member_vote_apply', blank=True)
    name = models.CharField(max_length=50, verbose_name='参赛名称')
    mobile = models.CharField(max_length=11, verbose_name='联系电话')
    images = models.ManyToManyField(Upload, verbose_name='参赛图片')
    introduce = models.CharField(max_length=500, verbose_name='参赛介绍')
    number = models.PositiveIntegerField(verbose_name='编号')
    count = models.PositiveIntegerField(verbose_name='票数', default=0)

    class Meta:
        db_table = 'activity_vote_apply'
        verbose_name = '投票活动报名信息'
        ordering = ('-count',)

    @staticmethod
    def get_instance_by_id(id):
        try:
            id = int(id)
        except:
            raise CustomError('无效的VoteActivityApply_id')

        instance = VoteActivityApply.objects.filter(pk=id).first()
        if not instance:
            raise CustomError('未找到对应的投票活动报名信息实例')
        return instance


class MemberVoteRecord(models.Model):
    vote_activity = models.ForeignKey(VoteActivity, verbose_name='当前活动', on_delete=models.PROTECT,
                                      related_name='member_vote_record')
    member = models.ForeignKey(Member, verbose_name='用户', on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name='投票时间', auto_now_add=True)

    class Meta:
        db_table = 'member_vote_record'
        verbose_name = '用户投票记录'
        ordering = ('-create_time',)
